//
//  ZhiShiViewController.m
//  Cunli
//
//  Created by 湛亚 on 15/6/19.
//  Copyright (c) 2015年 lilubin. All rights reserved.
//

#import "ZhiShiViewController.h"

#import "MyDataService.h"
#import "NSString+MD5.h"
#import "zhiShi.h"
#import "ZhiShiDetailViewController.h"
#import "MJRefresh.h"

@interface ZhiShiViewController (){
    
    NSMutableArray *zhiShiArray;
    float screenWidth;
    int contentPage;
    UITableView *tableview;
}

@end

@implementation ZhiShiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"小知识";
    contentPage=1;
    screenWidth=[UIScreen mainScreen].bounds.size.width;
    
    if ([self.zhiShiTableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.zhiShiTableView setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([self.zhiShiTableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.zhiShiTableView setLayoutMargins:UIEdgeInsetsZero];
    }
    
    zhiShiArray=[[NSMutableArray alloc]init];
    [self getZhiShiInfoWithPage:1 andBool:YES];
    [self addRefreshAttribute];
}

- (void)addRefreshAttribute
{
    __weak typeof(self) weakSelf = self;
    
    [self.zhiShiTableView addLegendHeaderWithRefreshingBlock:^{
        [weakSelf reloadDataToNew:YES]; //刷新
    }];
    
    [self.zhiShiTableView addLegendFooterWithRefreshingBlock:^{
        [weakSelf reloadDataToNew:NO]; //加载更多
    }];
    
}

- (void)reloadDataToNew:(BOOL)new{
    
    if (new) {
        contentPage=1;
        [self getZhiShiInfoWithPage:contentPage andBool:new];
    }else{
        contentPage=contentPage+1;
        [self getZhiShiInfoWithPage:contentPage andBool:new];
    }
}

-(void)getZhiShiInfoWithPage:(int)page andBool:(BOOL)new{
    
    NSMutableDictionary *CheckParams =[[NSMutableDictionary alloc]init];
    [CheckParams setObject:[Config getOwnUserID] forKey:@"userid"];
    [CheckParams setObject:[Config getOwnAreaids] forKey:@"areaids"];
    [CheckParams setObject:[NSString stringWithFormat:@"%d",page] forKey:@"page"];
    [CheckParams setObject:@"time_down" forKey:@"orderby"];
    [CheckParams setObject:@"10" forKey:@"pagesize"];
    [CheckParams setObject:@"4" forKey:@"type"];
    NSString *resultSign = [NSString getSignFromParam:CheckParams andSession:[Config getOwnSession]];
    [CheckParams setObject:resultSign forKey:@"sign"];
    [MyDataService requestURL:@"convenientlist.php" httpMethod:@"GET" params:CheckParams fileDatas:nil complection:^(id result) {
        
        NSLog(@"%@",result);
        [self.zhiShiTableView.legendHeader endRefreshing];
        [self.zhiShiTableView.legendFooter endRefreshing];
        
        if (![[result objectForKey:@"response"] isEqualToString:@"convenientlist"]) {
            
            MBProgressHUD *hud1 = [Config createHUD];
            hud1.mode = MBProgressHUDModeCustomView;
            hud1.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"error"]];
            hud1.labelText = @"获取失败!";
            [hud1 hide:YES afterDelay:1];
            return ;
        }
        
        NSMutableArray *arr=[result objectForKey:@"list"];
        if (arr.count==0) {
            
            if (!new) {
                MBProgressHUD *hud = [Config createHUD];
                hud.mode = MBProgressHUDModeCustomView;
                hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"done"]];
                hud.labelText = @"没有更多数据";
                [hud hide:YES afterDelay:1];
                contentPage=contentPage-1;
                [self.zhiShiTableView.legendFooter setState:MJRefreshFooterStateNoMoreData];
            }
            return ;
        }
        
        if (new) {
            [zhiShiArray removeAllObjects];
        }
        for (NSDictionary *dic in arr) {
            //zhiShi *zhishi=[[zhiShi alloc]initWithDictionary:dic];
            zhiShi *zhishi=[zhiShi yy_modelWithDictionary:dic];
            [zhiShiArray addObject:zhishi];
        }
        [self.zhiShiTableView reloadData];
        
    }failureAction:^{
        
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return zhiShiArray.count;
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 70;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIdentifier=@"cell";
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    if(!cell){
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        
    }
    
    zhiShi *zhishi=[zhiShiArray objectAtIndex:indexPath.row];
    
    UILabel *titleLab=[[UILabel alloc]initWithFrame:CGRectMake(10, 5, screenWidth-20, 50)];
    titleLab.numberOfLines=0;
    titleLab.text=zhishi.title;
    [cell addSubview:titleLab];
    
    UILabel *timeLab=[[UILabel alloc]initWithFrame:CGRectMake(screenWidth-160, 55, 150, 10)];
    timeLab.text=zhishi.addtime;
    timeLab.textAlignment=2;
    timeLab.font=[UIFont systemFontOfSize:13];
    [cell addSubview:timeLab];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    zhiShi *zhishi=[zhiShiArray objectAtIndex:indexPath.row];
    ZhiShiDetailViewController *zhiShiVC=[[ZhiShiDetailViewController alloc]init];
    zhiShiVC.itemid=zhishi.itemid;
    zhiShiVC.item_mid=zhishi.item_mid;
    zhiShiVC.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:zhiShiVC animated:YES];
    
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
